﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace VisaValeCheck
{
    internal static class SaldosSerialization
    {
        public static List<VisaSaldo> Saldos { get; private set; }

        private const string c_pwd = "Mamamia, here we go again! Mama! Mamamia!";

        static SaldosSerialization()
        {
            if (File.Exists("cartao.xml") && (Saldos == null))
            {
                XmlSerializer xml = new XmlSerializer(typeof(List<VisaSaldo>));
                using (FileStream f = File.Open("cartao.xml", FileMode.Open))
                {
#if !DEBUG
                    System.Security.Cryptography.Rijndael enc = System.Security.Cryptography.RijndaelManaged.Create();
                    System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                    enc.Key = md5.ComputeHash(Encoding.ASCII.GetBytes(c_pwd));
                    enc.IV = md5.ComputeHash(Encoding.ASCII.GetBytes(c_pwd));
                    using (System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(f, enc.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read))
                    {
                        try
                        {
                            Saldos = (List<VisaSaldo>)xml.Deserialize(encStream);
                        }
                        catch (Exception)
                        {
                            f.Close();
                            File.Delete("cartao.xml");
                        }
                    }
#else
                    try
                    {
                        Saldos = (List<VisaSaldo>)xml.Deserialize(f);
                    }
                    catch (Exception)
                    {
                        f.Close();
                        Saldos = new List<VisaSaldo>();
                        File.Delete("cartao.xml");
                    }
#endif
                }
            }
            else
                Saldos = new List<VisaSaldo>();
        }

        public static VisaSaldo Carregar(NumeroCartao numeroCartao)
        {
            var procura = from no in Saldos
                          where
                          no.NumeroCartao == numeroCartao
                          select no;
            VisaSaldo saldo = null;
            if (procura.Count() > 0)
                saldo = procura.First();
            return saldo;
        }

        public static VisaSaldo SaldoAtivo()
        {
            var procura = from no in Saldos
                          where
                          no.NumeroCartao.Selecionado == true
                          select no;
            VisaSaldo saldo = null;
            if (procura.Count() > 0)
                saldo = procura.First();
            return saldo;
        }

        public static void Deletar(NumeroCartao cartao)
        {
            var saldo = from no in Saldos
                        where
                        no.NumeroCartao == cartao
                        select no;
            if (saldo.Count() > 0)
                Saldos.Remove(saldo.First());
            Salvar();
        }

        public static void Salvar()
        {
            XmlSerializer xml = new XmlSerializer(typeof(List<VisaSaldo>));
#if !DEBUG
            System.Security.Cryptography.Rijndael enc = System.Security.Cryptography.RijndaelManaged.Create();
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            enc.Key = md5.ComputeHash(Encoding.ASCII.GetBytes(c_pwd));
            enc.IV = md5.ComputeHash(Encoding.ASCII.GetBytes(c_pwd));
            using (FileStream f = File.Create("cartao.xml", 1024))
            {
                using (System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(f, enc.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    xml.Serialize(encStream, Saldos);
                }
            }
#else
            using (FileStream f = File.Create("cartao.xml", 1024))
            {
                xml.Serialize(f, Saldos);
            }
#endif
        }

        public static void Salvar(VisaSaldo saldo)
        {
            Saldos.Add(saldo);
            Salvar();
        }
    }
}
